<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/3/22
 * Time: 下午5:16
 */
class LoginService extends Service
{

    public static function header($params){
        $header = array(
            'RecDate'       => array('text' => '日期'),
            'GameSrc'       => array('text' => '游戏'),
            'SID'           => array('text' => '游戏服'),
            'DAU'           => array('text' => '活跃人数', 'rule' => 'int'),
            'LoginUser'     => array('text' => '登入人数', 'rule' => 'int'),
            'LogoutUser'    => array('text' => '登出人数', 'rule' => 'int'),
            'AvgOnlineTime' => array('text' => '平均在线时长(秒)', 'rule' => 'int'),
            'PayLoginUser'  => array('text' => '付费用户登录人数', 'rule' => 'int'),
            'PayLoginRate'  => array('text' => '付费用户登录占比', 'rule' => 'percent')
        );
        
        if($params['server'] == 'ALL'){
        	unset($header['SID']);
        }

        return $header;
    }

    public static function data($params){
        if (PubFunc::checkEmpty($params, array('g', 'server', 'time_start', 'time_end'))) return array();

        $modGameLogin = new ModGameLogin();

        $data = $modGameLogin->getData($params);
        
        $new_data = array();
        foreach ($data as $key=>$val){
        	if($params['server'] != 'ALL'){
        		$new_data[$val['RecDate']]['SID'] = $val['SID'];
        	}
        	$new_data[$val['RecDate']]['RecDate'] = $val['RecDate'];
        	$new_data[$val['RecDate']]['GameSrc'] = $val['GameSrc'];       	
        	 
        	$new_data[$val['RecDate']]['LoginUser'] += $val['LoginUser'];
        	$new_data[$val['RecDate']]['LogoutUser'] += $val['LogoutUser'];
        	$new_data[$val['RecDate']]['PayLoginUser'] += $val['PayLoginUser'];
        	$new_data[$val['RecDate']]['ActiveUser'] += $val['ActiveUser'];
        	$new_data[$val['RecDate']]['DAU'] += $val['DAU'];
        	
        	$new_data[$val['RecDate']]['AvgOnlineTime'] += $val['AvgOnlineTime'];
        	$new_data[$val['RecDate']]['num'] += 1;

        }

        foreach($new_data as &$v){
            $v['PayLoginRate'] = number_format($v['PayLoginUser'] / $v['LoginUser'] * 100, 2) . '%';
            $v['AvgOnlineTime'] = $v['AvgOnlineTime'] / $v['num'];
        }
        unset($v);

        return $new_data;
    }

    public static function chart($data, $params){

        $chartData = array();

        foreach ($data as $v) {
            $chartData['活跃用户数'][substr($v['RecDate'], 5)] = intval($v['LoginUser']);
        }

        return array(
            'data' => $chartData,
            'config' => array(
                'series' => array(
                    '活跃用户数' => array(
                        'symbolSize' => 1,
                    )
                ),
            )
        );
    }

}